package com.ruoyi.web.controller.medicine;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.domain.HisMedicineDrug;
import com.ruoyi.service.IHisMedicineCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author rk
 * @description: TODO
 * @date 2024/9/1 16:41
 */
@RestController
@RequestMapping("/medicine/cart")
public class CartController extends BaseController {
    @Autowired
    private IHisMedicineCartService hisMedicineCartImpl;

    @PostMapping("/{drugIds}")
    public AjaxResult addCartById(@PathVariable Long[] drugIds) throws JsonProcessingException {
      return   toAjax(hisMedicineCartImpl.addDrugToCart(getUserId(),drugIds));
    }

    @GetMapping
    public AjaxResult getCartByUserId() throws JsonProcessingException {
        return success(hisMedicineCartImpl.selectCartByUserId(getUserId()));
    }


    @DeleteMapping("/{drugIds}")
    public AjaxResult deleteCarByDrugId(@PathVariable Long[] drugIds) throws JsonProcessingException {
        return toAjax(hisMedicineCartImpl.deleteCartByCarId(drugIds,getUserId()));
    }

    @PutMapping("/{drugId}/{durgCarNum}")
    public AjaxResult setCartDrugNum(@PathVariable Long drugId,@PathVariable Long durgCarNum) throws JsonProcessingException {
        HisMedicineDrug hisMedicineDrug = new HisMedicineDrug();
        hisMedicineDrug.setDrugId(drugId);
        hisMedicineDrug.setDrugCarNum(durgCarNum);
        return toAjax( hisMedicineCartImpl.setCartDrugNum(hisMedicineDrug,getUserId()));
    }

    @PutMapping("/setFactory/{drugId}/{factoryId}")
    public AjaxResult setCartDrugFactoryId(@PathVariable Long drugId,@PathVariable Long factoryId) throws JsonProcessingException {
        HisMedicineDrug hisMedicineDrug = new HisMedicineDrug();
        hisMedicineDrug.setDrugId(drugId);
        hisMedicineDrug.setFactoryId(factoryId);
        return toAjax( hisMedicineCartImpl.setCartDrugFactory(hisMedicineDrug,getUserId()));
    }
}
